<template>
    <div id="v-left-c">
        <div class="car-out">
            <!-- <ul class="car-s">
                <li><span class="text-s">一般风险</span><span class="car-info in-s">328</span></li>
                <li><span class="text-s">高危风险</span><span class="car-info out-s">123</span></li>
                <li><span class="text-s">严重风险</span><span class="car-info stay-s">294</span></li>
            </ul> -->
            <div style="width: 100%;height: 100%;" ref="carChar"></div>
        </div>
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, Ref, onMounted , watchEffect, reactive , toRaw , nextTick } from 'vue';
import * as echarts from 'echarts';
import ecTheme from "/@/assets/echartsConfig/westeros.project.json";
export default defineComponent({
    name:'v-left-c',
    props: {
        leftCenterData: {
            type: String,
            default: '',
        },
    },
    setup(props){
        const carChar:Ref = ref(null);
        const liData = ref();
        let isFinish = false;

        echarts.registerTheme('westeros', ecTheme);
        const initCharts = () => {
            let chart: any; 
            chart = echarts.init(carChar.value as HTMLElement, 'westeros');
            let option:any = {
                legend: {
                    bottom: 0,
                    left: 'center',
                    textStyle:{
                        color:'#FFFFFF',
                    },
                    itemWidth: 12,
                    itemHeight: 12,
                    padding: 0,
                    data: [],
                },
                grid: {
                    left: '2%',
                    top:10,
                    width: '96%',
                    bottom: 30,
                    containLabel: true
                },
                tooltip: {},
                dataset: {
                    dimensions: [],
                    source: []
                },
                xAxis: { 
                    type: 'category',
                    axisLine:{
                        lineStyle: {
                            color:'#3f6986',
                        }
                    },
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                        textStyle: {
                            color: "#FFFFFF"
                        } 
                    }
                },
                yAxis: {
                    splitLine: {
                        lineStyle: {
                            color: ['#3f6986']
                        }
                    },
                    axisLine:{
                        lineStyle: {
                            color:'#3f6986',
                        }
                    },
                    axisLabel: {
                        textStyle: {
                            color: "#FFFFFF"
                        } 
                    },
                    min: 0,
                    max: 100,
                },
                series: [
                    { 
                        type: 'bar',
                        barMaxWidth: '14%',
                        itemStyle: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                { offset: 0, color: 'rgb(6,204,253)' },
                                { offset: 1, color: 'rgb(6,74,104)' }
                            ])
                        }
                    }, 
                    { 
                        type: 'bar',
                        barMaxWidth: '14%',
                        itemStyle: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                { offset: 0, color: 'rgb(251,193,0)' },
                                { offset: 1, color: 'rgb(97,74,0)' }
                            ])
                        }
                    },
                    { 
                        type: 'bar',
                        barMaxWidth: '14%',
                        itemStyle: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                { offset: 0, color: 'rgb(244,34,34)' },
                                { offset: 1, color: 'rgb(91,3,3)' }
                            ])
                        }
                    },
                ]
            }
            let setTime = setInterval(() => {
                if(isFinish || liData.value){
                    let seriesData: any = [];
                    if(liData.value.length !== 0){
                        liData.value.map((i: any) => {
                            let seriesObj: any = {};
                            seriesObj.product = i.date.split(' ')[0];
                            if(i.riskAnalysis.length !== 0){
                                i.riskAnalysis.forEach((item: any) => {
                                    seriesObj[item.name] = item.count;
                                });
                            }
                            seriesData.push(seriesObj);
                        });
                    }
                    option.legend.data = [
                        {name: '可能危险，需要注意(2级)',icon: 'rect',itemStyle:{color:'rgb(6,206,255)'}},
                        {name: '显著危险，需要整改(3级)',icon: 'rect',itemStyle:{color:'rgb(255,195,0)'}},
                        {name: '高度危险，需要立即整改(4级)',icon: 'rect',itemStyle:{color:'rgb(255,36,36)'}},
                    ],
                    option.dataset = {
                        dimensions: ['product', '可能危险，需要注意(2级)', '显著危险，需要整改(3级)', '高度危险，需要立即整改(4级)'],
                    },
                    option.dataset.source = seriesData;
                    clearInterval(setTime);
                    return option && chart.setOption(option)
                }
            },20);
        }
        watchEffect(() => {
            let getData = props.leftCenterData;
            if(getData !== ''){
                let setArr = JSON.parse(getData);
                liData.value = setArr;
                isFinish = true;
                initCharts()
            }
        });
        onMounted(() => {
            nextTick(() => {
                initCharts()
            })
        })
        return {
            carChar,
        }
    }
})
</script>
<style lang="scss" scoped>
#v-left-c{
    width: 100%;
    height: 100%;
    ul,li{
        padding: 0;
        margin: 0;
        list-style: none;
    }
    .car-out{
        margin: 6vh auto 0 auto;
        height: 72%;
        width: 90%;
    }
    .car-s{
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        width: 76%;
        margin: auto;
        li{
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: center;  
        }
        .car-info,.text-s{
            display: block;
        }
        .car-info{
            width: 40px;
            height: 20px;
            text-align: center;
            line-height: 20px;
            margin-left: 5px;
        }
        .in-s{
            background-image: url(../../../image/in.png);
            background-repeat: no-repeat;
            background-size: 100% 100%;
            background-position: center;
        }
        .out-s{
            background-image: url(../../../image/out.png);
            background-repeat: no-repeat;
            background-size: 100% 100%;
            background-position: center;
        }
        .stay-s{
            background-image: url(../../../image/stay.png);
            background-repeat: no-repeat;
            background-size: 100% 100%;
            background-position: center;
        }
    }
}
</style>